﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using SharpDiary.Core.Utility;
using SharpDiary.Core.Controls;

namespace SharpDiary.Core.Common
{
    /// <summary>
    /// 实现 IGuiModule 接口的用户界面基类。
    /// </summary>
    public partial class GuiModuleUIBase : Form, IGuiModule
    {
        protected bool _autoSaveSettings = true; //是否自动保存相关配置信息
        protected string _keyName = string.Empty; //配置节点名称。
        private PopupMessageControl _popupMsgControl;

        private PopupMessageControl PopupMsgControl
        {
            get
            {
                if (_popupMsgControl == null || _popupMsgControl.IsDisposed)
                {
                    _popupMsgControl = new PopupMessageControl();
                    _popupMsgControl.Dock = DockStyle.Top;
                    this.Controls.Add(_popupMsgControl);
                }

                return _popupMsgControl;
            }
            set { _popupMsgControl = value; }
        }

        public GuiModuleUIBase()
        {
            InitializeComponent();


        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

        }

        #region IGuiModule 成员

        /// <summary>
        /// 显示用户界面。
        /// </summary>
        public virtual void Display()
        {
            if (!_isDialog)
            {
                this.Show();

                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.WindowState = FormWindowState.Normal;
                }

                this.Activate();
                this.BringToFront();
            }
            else
            {
                this.ShowDialog();
            }
        }

        public void Execute(object parameter, object sender)
        {
            SetParameter(parameter, sender);
            Display();
        }

        #endregion

        #region IModule 成员

        private IShell _shell;

        /// <summary>
        /// 获取或设置主框架实例。
        /// </summary>
        public IShell OwnerShell
        {
            get
            {
                return _shell;
            }
            set
            {
                _shell = value;
            }
        }

        /// <summary>
        /// 实例销毁前触发此事件。
        /// </summary>
        public event EventHandler<ModuleDisposedEventArgs> ModuleDisposed;

        /// <summary>
        /// 触发 ModuleDisposed 事件。
        /// </summary>
        protected virtual void OnModuleDisposed(object sender, ModuleDisposedEventArgs e)
        {
            if (ModuleDisposed != null) { ModuleDisposed(sender, e); }
        }

        /// <summary>
        /// 触发 MessageReceived 事件。
        /// </summary>
        public virtual void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            //do nothing
        }


        #endregion

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (_autoSaveSettings)
            {
                _keyName = GetSettingsKeyName();
                this.StartPosition = FormStartPosition.Manual;

                LoadSettings();
            }
        }

        #region IModule 成员


        public virtual void SetParameter(object parameter, object sender)
        {
            //do nothing.
        }

        /// <summary>
        /// 配置信息节点名称。
        /// </summary>
        public virtual string GetSettingsKeyName() { return string.Empty; }

        #endregion


        /// <summary>
        /// 保存配置信息。
        /// </summary>
        protected virtual void SaveSettings()
        {
            if (string.IsNullOrEmpty(_keyName)) { return; }

            //保存窗体显示位置信息。
            UserSettings.GetInstance().CreateOrUpdateSettingValue(_keyName + ".Location.X", this.Location.X);
            UserSettings.GetInstance().CreateOrUpdateSettingValue(_keyName + ".Location.Y", this.Location.Y);

            //保存窗体大小
            UserSettings.GetInstance().CreateOrUpdateSettingValue(_keyName + ".Size.Height", this.Size.Height);
            UserSettings.GetInstance().CreateOrUpdateSettingValue(_keyName + ".Size.Width", this.Size.Width);
        }

        /// <summary>
        /// 加载配置信息。
        /// </summary>
        protected virtual void LoadSettings()
        {
            if (string.IsNullOrEmpty(_keyName)) { return; }

            try
            {
                //窗体显示位置信息。
                int x = Convert.ToInt32(UserSettings.GetInstance().GetSettingValue(_keyName + ".Location.X", "0"));
                int y = Convert.ToInt32(UserSettings.GetInstance().GetSettingValue(_keyName + ".Location.Y", "0"));

                if (x != 0 && y != 0)
                {
                    this.Location = new Point(x, y);
                }
                else
                {
                    this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                        (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
                }

                //窗体大小
                int height = Convert.ToInt32(UserSettings.GetInstance().GetSettingValue(_keyName + ".Size.Height", this.Height.ToString()));
                int width = Convert.ToInt32(UserSettings.GetInstance().GetSettingValue(_keyName + ".Size.Width", this.Width.ToString()));

                this.Size = new Size(width, height);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (!this.Visible)
            {
                if (PopupMsgControl.Visible) { PopupMsgControl.Hide(); }
            }
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);

            if (PopupMsgControl.Visible) { PopupMsgControl.Hide(); }

            if (_autoSaveSettings)
            {
                if (this.Visible && this.WindowState != FormWindowState.Minimized)
                {
                    SaveSettings();
                }
            }
        }

        /// <summary>
        /// 延时关闭窗体。
        /// </summary>
        /// <param name="millionSeconds">毫秒</param>
        protected virtual void DelayClose(int millionSeconds)
        {
            timerClose.Interval = millionSeconds;
            timerClose.Start();
        }

        /// <summary>
        /// 延时1秒关闭窗体。
        /// </summary>
        protected virtual void DelayClose()
        {
            timerClose.Interval = 1000;
            timerClose.Start();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            switch (e.KeyCode)
            {
                case Keys.Escape:
                    {
                        Close();
                        break;
                    }
            }
        }

        public virtual void ShowMessage(string message)
        {
            if (string.IsNullOrEmpty(message)) { return; }

            PopupMsgControl.Show(message);
        }

        private void timerClose_Tick(object sender, EventArgs e)
        {
            timerClose.Stop();

            if (PopupMsgControl.Visible) { PopupMsgControl.Hide(); }
            Close();
        }

        #region IGuiModule 成员

        private bool _isDialog = false;

        /// <summary>
        /// 获取或设置是否模式显示。
        /// </summary>
        public bool IsDialog
        {
            get
            {
                return _isDialog;
            }
            set
            {
                _isDialog = value;
            }
        }

        #endregion
    }
}
